<template>
    <div class="app-container">
        <!-- 列表 -->
        <el-table :data="list" stripe>
            <el-table-column type="index" label="序号" width="60" align="center" />
            <el-table-column prop="name" label="借款人姓名" width="90" />
            <el-table-column prop="mobile" label="手机" />
            <el-table-column prop="amount" label="借款金额" />
            <el-table-column prop="params.period" label="借款期限" width="90">
            </el-table-column>
            <el-table-column prop="params.returnMethod" label="还款方式" width="150" />
            <el-table-column prop="params.moneyUse" label="资金用途" width="100" />
            <el-table-column prop="params.borrowYearRate" label="年化利率" width="90">
            </el-table-column>
            <el-table-column prop="params.status" label="状态" width="100" />
            <el-table-column prop="createTime" label="申请时间" width="150" />
            <el-table-column label="操作" width="150" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini">
                        <router-link :to="'/borrower/borrow/detail/' + scope.row.id">
                            查看
                        </router-link>
                    </el-button>
                    <el-button v-if="scope.row.status === 1" type="warning" size="mini" @click="approvalShow(scope.row)">
                        审批
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 审批对话框 -->
        <el-dialog title="审批" :visible.sync="dialogVisible" width="490px">
            <el-form label-position="right" label-width="100px">
                <el-form-item label="是否通过">
                    <el-radio-group v-model="borrowInfoApproval.status">
                        <el-radio :label="2">通过</el-radio>
                        <el-radio :label="-1">不通过</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="borrowInfoApproval.status == 2" label="标的名称">
                    <el-input v-model="borrowInfoApproval.title" />
                </el-form-item>
                <el-form-item v-if="borrowInfoApproval.status == 2" label="起息日">
                    <el-date-picker v-model="borrowInfoApproval.lendStartDate" type="date" placeholder="选择开始时间"
                        value-format="yyyy-MM-dd" />
                </el-form-item>
                <el-form-item v-if="borrowInfoApproval.status == 2" label="年化收益率">
                    <el-input v-model="borrowInfoApproval.lendYearRate">
                        <template slot="append">%</template>
                    </el-input>
                </el-form-item>
                <el-form-item v-if="borrowInfoApproval.status == 2" label="服务费率">
                    <el-input v-model="borrowInfoApproval.serviceRate">
                        <template slot="append">%</template>
                    </el-input>
                </el-form-item>
                <el-form-item v-if="borrowInfoApproval.status == 2" label="标的描述">
                    <el-input v-model="borrowInfoApproval.lendInfo" type="textarea" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false"> 取消 </el-button>
                <el-button type="primary" :disabled="submitButtonDisabled" @click="approvalSubmit"> 确定 </el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import api from '@/api/borrower'
export default {
    data() {
        return {
          list: [],
          borrowInfoApproval: {
            lendYearRate: 12,
            serviceRate: 5,
            title: ''
          },
          dialogVisible: false,
          submitButtonDisabled: false

        }
    },
    methods: {
      //提交审批数据的方法
      approvalSubmit(){
        //禁止重复提交
        this.submitButtonDisabled = true
        api.approvalLend(this.borrowInfoApproval).then(res=>{
          this.$message.success('审批成功!')
          this.dialogVisible = false
          //重新刷新数据
          this.initData()
        })
      },
      //打开审批窗口的方法
      approvalShow(row){
        //启用审批的窗口
        this.dialogVisible = true
        //将该行数据的id赋值给borroweInfoApproval 也就是将borrowInfo 的 id 赋值给数据模型对象
        this.borrowInfoApproval.borrowInfoId = row.id
        //自动的根据moneyUse生成标的的名称
        this.borrowInfoApproval.title = row.params.moneyUse + '贷' + Date.parse(new Date()) + '号'
      },
      initData(){
        api.getBorrowLists().then(res=>{
          // console.log(res.data.items)
          // debugger
          this.list = res.data.items
        })
      }
    },
  created() {
      this.initData()
  },
  mounted() {

    }
}
</script>
